package com.zz.framework.core.http

import com.zz.framework.BuildConfig
import com.zz.framework.core.Kernel
import com.zz.framework.core.annotation.BaseUrl

/**
 * Copyright © 2020 zanezhao All rights reserved.
 * Created by zanezhao on 2020/3/1 1:19 PM.
 * @author: zanezhao 1820022519@qq.com
 * @version: V1.0
 */
object RetrofitFactory {

    fun <T> create(clz: Class<T>): T {
        prepareBaseUrl(clz)
        val retrofit = Kernel.retrofitConfig.initRetrofit()
        return retrofit.create(clz)
    }

    private fun <T> prepareBaseUrl(clz: Class<T>) {
        val baseUrlAnnotation = clz.getAnnotation(BaseUrl::class.java)
        val baseUrl = baseUrlAnnotation?.baseUrl ?: throw IllegalArgumentException("base url is null")
        val debugBaseUrl = baseUrlAnnotation?.debugBaseUrl ?: throw IllegalArgumentException("base url is null")
        Kernel.retrofitConfig.baseUrl = if (BuildConfig.DEBUG) { //测试服务器
            debugBaseUrl
        } else { //正式地址
            baseUrl
        }
    }
}